#ifndef YCONTINUOUSANIMATION_H
#define YCONTINUOUSANIMATION_H

/*To be animated, an object must be able to be
	1: Copied
	2: Multiplied by a float
	3: Added to another object of the same type
*/

#include "AnimationBase.h"

namespace Y{

template <class C>
class ContinuousAnimation: public AnimationBase{

	public:
		
		C *animatedObject;
		C delta;
		float falloff;
		bool ended;
		
	public:
	
		ContinuousAnimation( Entity *parentis = NULL ):
			AnimationBase	(parentis),
			animatedObject	(NULL),
			falloff			(1.1)
		{
			connect( "tick", this, FUNC(Entity,update) );
		}
		
		virtual void handleConfig ( ConfigVector& cv ){
			for ( int i = 0; i < cv.size(); ++i ){
				if ( cv[i].name == "object ptr" ){ animatedObject = cv[i].to<C *>();handled(); }
				else if ( cv[i].name == "delta" || cv[i].name == "change by" ){
					delta = cv[i].to<C>();handled();
				}
				else CH( "falloff", falloff , float )
				else CH( "ended", ended , bool )
			}
			AnimationBase::handleConfig(cv);
		}
		
		virtual void object ( C& obj ){ animatedObject = &obj; }
		virtual void object ( C* obj ){ animatedObject = obj; }
		virtual C& object () { return *animatedObject; }
		
		virtual void start (){
			AnimationBase::start();
		}
		
		virtual void stop (){ ended = true; }
		
		virtual bool tick ( float f = 1.0 ){
			if ( ! ended ){
				if ( speed < 1.0 ){
					speed *= falloff;
				}
			}
			else{
				if ( speed > 0.01 ){
					speed /= falloff;
				}
				else{
					return false;
				}
			}


			*animatedObject = (*animatedObject + ( delta * f ));
			return true;
		}
};

}

#endif
